package com.qf.flash.ui.home;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.http.HttpManager;
import com.qf.flash.model.Pay2JoinBean;
import com.qf.flashlib.util.RxUtils;
import com.qf.flashlib.widget.dialog.BaseDialog;
import com.qf.flashlib.widget.dialog.ViewHolder;

import io.reactivex.disposables.Disposable;

/**
 * 2020/2/25 10:53
 * 付钱加入游戏
 */
public class Pay2JoinDialogFragment extends BaseDialog implements View.OnClickListener {
    private Pay2JoinBean bean;

    public static Pay2JoinDialogFragment newInstance(Pay2JoinBean bean) {
        Pay2JoinDialogFragment fragment = new Pay2JoinDialogFragment();
        Bundle b = new Bundle();
        b.putParcelable("data", bean);
        fragment.setArguments(b);
        return fragment;
    }
    private Disposable disposable;

    @Override
    public int setUpLayoutId() {
        return R.layout.fragment_pay_join;
    }

    @Override
    public void convertView(ViewHolder holder, BaseDialog dialog) {
        Bundle arguments = getArguments();
        if (arguments == null)
            return;
        bean = arguments.getParcelable("data");
        if (bean == null)
            return;
        setSize(300, 0);
        holder.setText(R.id.tv_name, "队名: " + bean.getGroupName());
        holder.setText(R.id.tv_password, "密码: " + bean.getPassword());
        holder.setText(R.id.tv_count, "人数: " + bean.getCount());
        holder.setText(R.id.tv_time, "时间: " + bean.getTime());
        holder.setText(R.id.tv_business, "商家: " + bean.getBusiness());
        holder.setText(R.id.tv_pay, "付定金: " + bean.getMoney());

        holder.getView(R.id.tv_pay).setOnClickListener(this);
        holder.getView(R.id.tv_cancel).setOnClickListener(this);
        getTips(holder.getView(R.id.tv_tips));
    }

    private void getTips(TextView tvTips) {
        disposable = HttpManager.getInstance().getGsonHttpApi()
                .SPONSOR_TIPS(UserMgr.getInstance().getLoginUser().getUid())
                .compose(RxUtils.handleResult())
                .compose(RxUtils.io_main())
                .subscribe(tipsBean -> tvTips.setText(tipsBean.getTip()));
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_pay:
                OrderPayActivity.start(getActivity(), bean);
                dismiss();
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (disposable != null)
            disposable.dispose();
    }
}
